from django import forms
from django.db import models

from project_manage.libs.storage.widgets import UpLoadFIleWidget, ImageUploadFKWidget, ImageHiddenWidget, TimeRangWidget


class UpLoadFIleField(models.CharField):
    def __init__(self, max_length=200, *args, **kwargs):
        super(UpLoadFIleField, self).__init__(max_length=200, *args, **kwargs)

    def formfield(self, **kwargs):
        return super().formfield(**{
            'form_class': UpLoadFIleFormField,
            **kwargs,
        })


class UpLoadFIleFormField(forms.fields.CharField):
    def __init__(self, *args, **kwargs):
        kwargs.update({'widget': UpLoadFIleWidget()})
        self.hidden_widget = ImageHiddenWidget()
        super().__init__(*args, **kwargs)


class ImageUploadFKField(models.ForeignKey):

    def __init__(self, to="storage.ImageStorage", on_delete=models.SET_NULL, related_name=None, related_query_name=None,
                 limit_choices_to=None, parent_link=False, to_field=None, db_constraint=True, **kwargs):
        super().__init__(to, on_delete, related_name, related_query_name, limit_choices_to, parent_link, to_field,
                         db_constraint, **kwargs)

    def formfield(self, **kwargs):
        return super().formfield(**{
            'form_class': ImageUploadFKFormField,
            **kwargs,
        })


class ImageUploadFKFormField(forms.ModelChoiceField):
    def __init__(self, *args, **kwargs):
        kwargs.update({'widget': ImageUploadFKWidget()})
        self.hidden_widget = ImageHiddenWidget()
        super().__init__(*args, **kwargs)


class TimeRangField(models.Field):
    """
    时间字段 如（'8:30' or ‘8:30 - 10.06’ ）
    """

    def __init__(self, max_length=15, *args, **kwargs):
        super(TimeRangField, self).__init__(max_length=15, *args, **kwargs)

    def formfield(self, **kwargs):
        return super().formfield(**{
            'form_class': TimeRangFormField,
            **kwargs,
        })


class TimeRangFormField(forms.Field):
    def __init__(self, *args, **kwargs):
        kwargs.update({'widget': TimeRangWidget()})
        # self.hidden_widget = ImageHiddenWidget()
        super().__init__(*args, **kwargs)
